package mapEditor.obj;

import debug.*;

import mapEditor.svg.SVGDOMTree;
import mapEditor.utils.MapLocation;
import mapEditor.utils.SvgStroke;
import mapEditor.utils.Util;

import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPanel;

//import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.dom.svg.SVGOMLineElement;
import org.apache.batik.dom.anim.AnimationTarget;
import org.apache.batik.dom.anim.AnimationTargetListener;
//import org.w3c.dom.svg.SVGElement;
import org.w3c.dom.svg.SVGLineElement;


// DOC: http://www.w3.org/TR/SVG11/shapes.html#LineElement
// DOC: http://www.w3.org/TR/SVG11/types.html#InterfaceSVGAnimatedLength
// DOC: http://www.zvon.org/xxl/svgReference/Output/el_line.html
public class LineObject extends DrawObject
{ //protected static final boolean USE_AnimatedLength=false; // OVERRIDE (to make updateSVGElement work)
 
  public LineObject(SVGDOMTree domTree,MapLocation location1,MapLocation location2)
  { super(domTree,domTree.appendSVGLineElement());
    stroke.setEnabled(true);
	add(location1);
	add(location2);
	String svgNS=null;//SVGDOMImplementation.SVG_NAMESPACE_URI;
	((SVGOMLineElement)svgElement).addTargetListener(svgNS,"x1",false,animationTargetListener);
	((SVGOMLineElement)svgElement).addTargetListener(svgNS,"y1",false,animationTargetListener);
	((SVGOMLineElement)svgElement).addTargetListener(svgNS,"x2",false,animationTargetListener);
	((SVGOMLineElement)svgElement).addTargetListener(svgNS,"y2",false,animationTargetListener);
  }
  AnimationTargetListener animationTargetListener=new AnimationTargetListener()
  { public void baseValueChanged(AnimationTarget t,String ns,String ln,boolean isCSS)
    { Debug.TRACE("LineObject.baseValueChanged: attribute="+ln+", target="+t);
	  
    }  
  };
  
  public LineObject(SVGDOMTree domTree,SVGLineElement svgElement)
  { super(domTree,svgElement);
	decodeSVG(svgElement);
  }
  
  public void decodeSVG(SVGLineElement svgElement)
  { Debug.TRACE("LineObject.decodeSVG: svgElement="+svgElement);
    this.setIdent(svgElement.getAttribute("id"));
    this.stroke=new SvgStroke(svgElement);
    clearPointSet();
    double x1=getValue(svgElement.getX1());
    double y1=getValue(svgElement.getY1());
    double x2=getValue(svgElement.getX2());
    double y2=getValue(svgElement.getY2());
    add(MapLocation.newPixelPoint(domTree.getMapGrid(),x1,y1));
    add(MapLocation.newPixelPoint(domTree.getMapGrid(),x2,y2));
  }
  
  protected void directUpdateSVGElement()
  {	MapLocation p1=getMapLocation(0);
	MapLocation p2=getMapLocation(1);
	String svgNS=null;//SVGDOMImplementation.SVG_NAMESPACE_URI;
	svgElement.setAttributeNS(svgNS,"id",getIdent());
    if(USE_AnimatedLength)
    { SVGOMLineElement line=(SVGOMLineElement)svgElement;
//    SVGLineElement line=(SVGLineElement)svgElement;
//    ebug.println("LineObject.updateSVGElement: isReadonly="+line.isReadonly());
      setValue(line.getX1(),p1.getX());
      setValue(line.getY1(),p1.getY());
      setValue(line.getX2(),p2.getX());
      setValue(line.getY2(),p2.getY());
    }
    else
    { svgElement.setAttributeNS(svgNS,"x1",""+p1.getX());
      svgElement.setAttributeNS(svgNS,"y1",""+p1.getY());
      svgElement.setAttributeNS(svgNS,"x2",""+p2.getX());
      svgElement.setAttributeNS(svgNS,"y2",""+p2.getY());    	
    }
    stroke.appendSVGAttributes(svgElement);
  }

  
  // ****************************************************************
  // *** INNER CLASS: EditGeneralPanel
  // ****************************************************************
  public JPanel getEditGeneralPanel(String title)
  { return(new EditGeneralPanel(title)); }
  private class EditGeneralPanel extends JPanel
  { static final long serialVersionUID=0; // AD'HOC
    private JTextField identField;
    private JButton strokeButton;
    
    public void paint(Graphics g)
    { Debug.TRACE("LineObject.EditGeneralPanel.paint: ");
      identField.setText(getIdent());
      strokeButton.setBackground(stroke.getColor());
  	  super.paint(g);  
    }
  	
    public EditGeneralPanel(String title)
    { setBorder(Util.getTitleBorder(title));
      setPreferredSize(new Dimension(350,50+20));
      setLayout(new GridLayout(2,2));
      add(new JLabel("Ident:"));
      identField=new JTextField(getIdent());
      identField.addActionListener(new ActionListener()
      { public void actionPerformed(ActionEvent e)
        { setIdent(identField.getText()); }
      });
      add(identField);
      strokeButton=addEditStrokeButton(this,true);
    }
  }    

}
